package com.cao.meet.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;
import android.view.View;
import android.widget.Button;

import com.blankj.utilcode.util.LogUtils;
import com.cao.meet.R;
import com.cao.meet.bean.Beauty;

import org.dom4j.DocumentException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class PullParseXMLActivity extends AppCompatActivity {

    private List<Beauty> mList;
    private Beauty mBeauty;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pull_parse_xml);
        final Button pull_print = (Button) findViewById(R.id.pull_print);
        pull_print.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pullAndPrint();
            }
        });
        final Button update_app = (Button) findViewById(R.id.update_app);
        update_app.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    updateApp();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (XmlPullParserException e) {
                    e.printStackTrace();
                } catch (DocumentException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void updateApp() throws IOException, XmlPullParserException, DocumentException {
//        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
//                "<updateinfo><app name=\"CHEMS\"><version>10</version><description><![CDATA[版本更新说明:]]></description><processkill><file>chems.exe</file></processkill><download><file unzip=\"true\">ChemsUpdate@20170118.zip</file></download><execute><file>autorun.bat</file><file>chems.exe</file></execute></app></updateinfo>";
//        String json = XmlAnaly.xmlChangeJson(xml);
//        LogUtils.e("json",json.trim());
        InputStream inputStream = getAssets().open("app_update.xml");
        XmlPullParser pullParser = Xml.newPullParser();
        pullParser.setInput(inputStream, "UTF-8");
        int eventType = pullParser.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
                case XmlPullParser.START_DOCUMENT:
                    break;
                case XmlPullParser.START_TAG:
                    if (pullParser.getName().equalsIgnoreCase("app")) {
                        String name = pullParser.getAttributeValue(0);
                        Log.e("name", name);
                    } else if (pullParser.getName().equalsIgnoreCase("version")) {
                        pullParser.next();
                        String version = pullParser.getText();
                        Log.e("version", version);
                    }else if (pullParser.getName().equalsIgnoreCase("description")){
//                        String description = pullParser.getText();
//                        Log.e("description",description);
                    }else if (pullParser.getName().equalsIgnoreCase("processkill")){

                    }else if (pullParser.getName().equalsIgnoreCase("file")){
                        int attributeCount = pullParser.getAttributeCount();
                        Log.e("attributeCount",attributeCount+"");
                        if (attributeCount != 0){
                            String attr = pullParser.getAttributeValue(0);
                            String file_name = pullParser.getText();
                            LogUtils.e("file_name",attr+"----"+file_name);
                        }
                    }
                    break;
            }
            eventType = pullParser.next();
        }
    }

    private void pullAndPrint() {
        try {
            //从Assets中获取xml文件并转为输入流
            InputStream inputStream = getAssets().open("language.xml");
            //获取XmlPullParser对象
            XmlPullParser pullParser = Xml.newPullParser();
            pullParser.setInput(inputStream, "UTF-8");
            //获取解析xml文件时的事件
            int eventType = pullParser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    //开始解析
                    case XmlPullParser.START_DOCUMENT:
                        mList = new ArrayList<>();
                        break;
                    case XmlPullParser.START_TAG://开始解析标签
                        if (pullParser.getName().equals("beauty")) {
                            mBeauty = new Beauty();
                        } else if (pullParser.getName().equals("name")) {
                            eventType = pullParser.next();//让解析器指针指向name属性
                            mBeauty.setName(pullParser.getText());
                        } else if (pullParser.getName().equals("age")) {
                            eventType = pullParser.next();//让解析器指针指向age属性
                            mBeauty.setAge(pullParser.getText());
                        }
                        break;
                    case XmlPullParser.END_TAG://标签解析完毕
                        if (pullParser.getName().equals("beauty")) {
                            mList.add(mBeauty);
                            mBeauty = null;
                        }
                        break;
                }
                eventType = pullParser.next();
            }
            for (int i = 0; i < mList.size(); i++) {
                Log.e("my lvoe=========", mList.get(i).toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
    }
}
